package cn.itcast.bos.base.action;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaBuilder.In;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.ws.rs.core.MediaType;

import org.apache.commons.lang.StringUtils;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;

import cn.itcast.bos.base.service.FixedAreaService;
import cn.itcast.bos.domain.base.Courier;
import cn.itcast.bos.domain.base.FixedArea;
import cn.itcast.bos.domain.base.TakeTime;
import cn.itcast.crm.domain.Customer;

@Controller
@Namespace("/")
@ParentPackage("json-default")
@Scope("prototype")
public class FixedAreaAction extends BaseAction<FixedArea>{
	
	@Autowired FixedAreaService fixedAreaService;
	List<Integer> customerIds;
	private Integer courierId;
	private Integer takeTimeId;
	
	public void setCourierId(Integer courierId) {
		this.courierId = courierId;
	}

	public void setTakeTimeId(Integer takeTimeId) {
		this.takeTimeId = takeTimeId;
	}

	public void setCustomerIds(List<Integer> customerIds) {
		this.customerIds = customerIds;
	}

	@Action(value="AddFixedArea")
	public String AddFixedArea(){
		fixedAreaService.AddFixedArea(t);
		return NONE;
	}
	
	@Action(value="showFixedArea",results={@Result(type="json",name="success")})
	public String showFixedArea(){
		Specification<FixedArea> spec=new Specification<FixedArea>() {
			List<Predicate> list=new ArrayList<Predicate>();
			@Override
			public Predicate toPredicate(Root<FixedArea> root, CriteriaQuery<?> query,
					CriteriaBuilder cb) {
				// TODO Auto-generated method stub
				if(StringUtils.isNotBlank(t.getId())){
					Predicate p1 = cb.like(root.get("id").as(String.class), "%"+t.getId()+"%");
					list.add(p1);	
				}if(StringUtils.isNotBlank(t.getCompany())){
					Predicate p2 = cb.like(root.get("company").as(String.class), "%"+t.getCompany()+"%");
					list.add(p2);
				}
				return cb.and(list.toArray(new Predicate[0]));
			}
		};
		Pageable pageable=new PageRequest(page-1, rows);
		Page page=fixedAreaService.showFixedArea(spec, pageable);
		putIntoValueStack(page);
		return SUCCESS;
	}
	
	@Action(value="customernotassociation",results={@Result(name="success",type="json")})
	public String customernotassociation(){
		Collection<? extends Customer> collection = WebClient.create("http://localhost:8082/crm_management/service/crm/customerservice/customer").
				query("associated", true).
				accept(MediaType.APPLICATION_JSON).getCollection(Customer.class);
		ActionContext.getContext().getValueStack().push(collection);
		return SUCCESS;
	}
	
	@Action(value="customerbyfixedid",results={@Result(name="success",type="json")})
	public String customerbyfixedid(){
		Collection<? extends Customer> collection = WebClient.create("http://localhost:8082/crm_management/service/crm/customerservice/fixedarea/"+t.getId()+"/customer").
		accept(MediaType.APPLICATION_JSON).getCollection(Customer.class);
		ActionContext.getContext().getValueStack().push(collection);
		return SUCCESS;
	}
	
	@Action(value="getCourier",results={@Result(name="success",type="json")})
	public String getCourier(){
		List<Courier> list=fixedAreaService.getgetCourier();
		ActionContext.getContext().getValueStack().push(list);
		return SUCCESS;
	}
	@Action(value="getTaketime",results={@Result(name="success",type="json")})
	public String getTaketime(){
		List<TakeTime> list=fixedAreaService.getTaketime();
		ActionContext.getContext().getValueStack().push(list);
		return SUCCESS;
	}
	
	@Action(value="fixedArea_associationCourierToFixedArea",results={@Result(type="redirect",name="success",location="/pages/base/fixed_area.html")})
	public String fixedArea_associationCourierToFixedArea(){
		fixedAreaService.fixedArea_associationCourierToFixedArea(t.getId(),courierId,takeTimeId);
		return SUCCESS;
	}
	@Action(value="associate",results={@Result(name="success",type="redirect",location="/pages/base/fixed_area.html")})
	public String associate(){
		System.out.println(customerIds);
		if(customerIds==null){
			customerIds=new ArrayList<Integer>();
			WebClient.create("http://localhost:8082/crm_management/service/crm/customerservice/fixedarea/"+t.getId()+"/customer").
			query("cids", customerIds).accept(MediaType.APPLICATION_JSON).type(MediaType.APPLICATION_JSON).put(null);
		}else{
			WebClient.create("http://localhost:8082/crm_management/service/crm/customerservice/fixedarea/"+t.getId()+"/customer").
			query("cids", customerIds).accept(MediaType.APPLICATION_JSON).type(MediaType.APPLICATION_JSON).put(null);
		}
		return SUCCESS;
	}
	
//type 	Set HTTP Content-Type header
//accept Set HTTP Accept header
//query	Update the current URI query parameters 设置请求参数
}
